package net.qhzw.vip.qiyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.SysTenantIdentity;
import net.qhzw.vip.qiyu.handler.MyException;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.result.ResultCode;
import net.qhzw.vip.qiyu.service.SysTenantIdentityService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Api(tags = "租户身份管理")
@RestController
@RequestMapping("sys/tenantIdentity")
public class SysTenantIdentityController {


    @Autowired
    private SysTenantIdentityService sysTenantIdentityService;


    @ApiOperation(value = "分页查询租户身份")
    @GetMapping
    public JsonResult<Page<SysTenantIdentity>> findByPage(Page<SysTenantIdentity> page, String identityName, Byte status) {
        Page<SysTenantIdentity> tenantIdentityPage = sysTenantIdentityService.page(page,
                new LambdaQueryWrapper<SysTenantIdentity>().
                        eq(status != null, SysTenantIdentity::getStatus, status).
                like(!StringUtils.isEmpty(identityName), SysTenantIdentity::getIdentityName, identityName));
        return JsonResult.ok(tenantIdentityPage);
    }

    @ApiOperation(value = "根据ID查询租户身份")
    @GetMapping("{id}")
    public JsonResult<SysTenantIdentity> findTenantIdentity(@PathVariable Long id) {
        SysTenantIdentity tenantIdentity = sysTenantIdentityService.getById(id);
        return JsonResult.ok(tenantIdentity);
    }

    @ApiOperation(value = "查询租户身份列表")
    @GetMapping("list")
    public JsonResult<List<SysTenantIdentity>> findList(String identityName, Byte status) {
        List<SysTenantIdentity> tenantIdentityList = sysTenantIdentityService
                .list(new LambdaQueryWrapper<SysTenantIdentity>()
                        .eq(status != null, SysTenantIdentity::getStatus, status)
                        .like(!StringUtils.isEmpty(identityName), SysTenantIdentity::getIdentityName, identityName));
        return JsonResult.ok(tenantIdentityList);
    }

    @ApiOperation(value = "添加租户身份")
    @PostMapping
    public JsonResult addTenantIdentity(@RequestBody SysTenantIdentity tenantIdentity) {
        check(tenantIdentity);
        SysTenantIdentity dbtenantIdentity = modifyTenantIdentity(tenantIdentity);
        boolean save = sysTenantIdentityService.save(dbtenantIdentity);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "修改租户身份")
    @PutMapping
    public JsonResult updateSysTenantIdentity(@RequestBody SysTenantIdentity sysTenantIdentity) {
        SysTenantIdentity dbtenantIdentity = modifyTenantIdentity(sysTenantIdentity);
        boolean update = sysTenantIdentityService.updateById(dbtenantIdentity);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "设置租户身份状态")
    @PutMapping("setStatus")
    public JsonResult setStatus(@RequestBody SysTenantIdentity tenantIdentity) {
        SysTenantIdentity dbtenantIdentity = modifyTenantIdentity(tenantIdentity);
        boolean update = sysTenantIdentityService.updateById(dbtenantIdentity);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "删除租户身份")
    @DeleteMapping("{id}")
    public JsonResult del(@PathVariable String id) {
        List<String> list = Arrays.asList(id.split(","));
        boolean remove = sysTenantIdentityService.removeByIds(list);
        if (remove) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    /***
     * 回填默认值
     * @param tenantIdentity
     * @return
     */
    private SysTenantIdentity modifyTenantIdentity(SysTenantIdentity tenantIdentity) {
        if (tenantIdentity == null) {
            throw new MyException(ResultCode.VALIDATE_FAILED);
        }
        if (tenantIdentity.getId() == null) {
            tenantIdentity.setId(IdWorker.getId());
            tenantIdentity.setCreated(new Date());
            tenantIdentity.setStatus((byte) 1);
        }
        tenantIdentity.setModifyBy(SecurityUtils.getUserId());
        return tenantIdentity;
    }

    /***
     * 参数校验
     * @param tenantIdentity
     */
    private void check(SysTenantIdentity tenantIdentity) {
        int count = sysTenantIdentityService.count(new LambdaQueryWrapper<SysTenantIdentity>()
                .eq(SysTenantIdentity::getIdentityName, tenantIdentity.getIdentityName()));
        if (count > 0) {
            throw new MyException(500, "身份已存在");
        }
    }

}
